﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinformCommonDemo.CommControlOperate;
using WinformCommonDemo.DataModel;

namespace WinformCommonDemo
{
    public partial class MainUI : Form
    {
        public MainUI()
        {
            InitializeComponent();
        }

        private void my_DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (my_DataGridView.Rows.Count > 0)
            {
                if (my_DataGridView.CurrentCell.Value.ToString() == "删除")
                {
                    DataGridViewHelper.DelSingleDataGridView<DataListModel>(my_DataGridView);
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //列表样式初始化
            InitDataGridView();
        }

        private void LoadDataList()
        {
            string path = Path.Combine(Directory.GetCurrentDirectory(), "DataList.txt");
            string json = TxtOperateHelper.ReadTxt(path);
            List<DataListModel> list = new List<DataListModel>();
            list = json.ToObj<List<DataListModel>>();
            if (list.Count > 0)
            {
                //初始化
                DataGridViewHelper.BindDataGridView(my_DataGridView, list);
                DataGridViewHelper.BindDataGridView(dataGridView1, list);
            }
        }

        private void InitDataGridView()
        {
            string path = Path.Combine(Directory.GetCurrentDirectory(), "DataGridViewData.txt");
            string json = TxtOperateHelper.ReadTxt(path);
            List<DataGridViewModel> list = new List<DataGridViewModel>();
            list = json.ToObj<List<DataGridViewModel>>();
            if (list.Count > 0)
            {
                //初始化
                DataGridViewHelper.InitDataGridView(my_DataGridView, list);
                DataGridViewHelper.InitDataGridView(dataGridView1, list);
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                if (dataGridView1.CurrentCell.Value.ToString() == "删除")
                {
                    DataGridViewHelper.DelSingleDataGridView<DataListModel>(dataGridView1);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<DataListModel> datas = DataGridViewHelper.SaveDataGridView<DataListModel>(my_DataGridView);
            if (datas.Count > 0)
            {
                //这里将数据集 datas 转存到需要的数据库即可；
                MessageBox.Show("数据保存成功", "提示", MessageBoxButtons.OK);
            }
            else
            {
                MessageBox.Show("暂无要保存的数据", "提示", MessageBoxButtons.OK);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataGridViewHelper.ClearDataGridView(my_DataGridView);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //加载数据
            LoadDataList();
        }
    }
}
